package com.cruise.game.controller.agent;

import com.cruise.game.common.api.ApiResult;
import com.cruise.game.common.constants.CommonConstants;
import com.cruise.game.controller.BaseController;
import com.cruise.game.model.param.user.WithdrawParam;
import com.cruise.game.model.vo.common.PageResult;
import com.cruise.game.model.vo.user.WithdrawalVO;
import com.cruise.game.service.agent.AgentWithdrawalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 代理提现控制器
 *
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Api(tags = "代理提现相关接口")
@RestController
@RequestMapping("/api/agent/withdraw")
@PreAuthorize("hasRole('ROLE_AGENT')")
public class AgentWithdrawalController extends BaseController {

    @Resource
    private AgentWithdrawalService agentWithdrawalService;

    /**
     * 提交提现申请
     */
    @ApiOperation("提交提现申请")
    @PostMapping
    public ApiResult<Long> submitWithdrawalApplication(
            @ApiParam(value = "提现信息", required = true)
            @Validated @RequestBody WithdrawParam param) {
        Long userId = getCurrentUserId();
        Long withdrawalId = agentWithdrawalService.submitWithdrawalApplication(userId, param);
        return ApiResult.success("提现申请已提交", withdrawalId);
    }

    /**
     * 获取提现申请列表
     */
    @ApiOperation("获取提现申请列表")
    @GetMapping
    public ApiResult<PageResult<WithdrawalVO>> getWithdrawalList(
            @ApiParam(value = "申请状态", required = false)
            @RequestParam(value = "status", required = false) String status,

            @ApiParam(value = "页码", required = false, defaultValue = "1")
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,

            @ApiParam(value = "每页条数", required = false, defaultValue = "10")
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        // 限制最大页面大小
        if (pageSize > CommonConstants.MAX_PAGE_SIZE) {
            pageSize = CommonConstants.MAX_PAGE_SIZE;
        }

        Long userId = getCurrentUserId();
        return ApiResult.success(
                agentWithdrawalService.getWithdrawalList(userId, status, page, pageSize)
        );
    }

    /**
     * 取消提现申请
     */
    @ApiOperation("取消提现申请")
    @PostMapping("/{withdrawalId}/cancel")
    public ApiResult<Boolean> cancelWithdrawalApplication(
            @ApiParam(value = "提现申请ID", required = true)
            @PathVariable("withdrawalId") Long withdrawalId) {
        Long userId = getCurrentUserId();
        return ApiResult.success(
                "提现申请已取消",
                agentWithdrawalService.cancelWithdrawalApplication(userId, withdrawalId)
        );
    }

    /**
     * 获取提现统计
     */
    @ApiOperation("获取提现统计")
    @GetMapping("/statistics")
    public ApiResult<Object> getWithdrawalStatistics() {
        Long userId = getCurrentUserId();
        return ApiResult.success(agentWithdrawalService.getWithdrawalStatistics(userId));
    }
}